package com.mt.api.modules.manager.impl;

import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.mt.api.modules.domain.AppBossBrand;
import com.mt.api.modules.domain.AppBrand;
import com.mt.api.modules.manager.IBrandManager;
import com.mt.api.modules.mapper.AppBossBrandMapper;
import com.mt.api.modules.mapper.AppBrandMapper;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;

import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Author: plover
 * @Date: 2022-01-02
 * @Description:
 */
@Component
@RequiredArgsConstructor
public class BrandManagerImpl extends ServiceImpl<AppBrandMapper, AppBrand> implements IBrandManager {

    private final AppBossBrandMapper bossBrandMapper;

    @Override
    public List<AppBrand> getBrandByIds(List<Integer> ids) {
        return baseMapper.selectBatchIds(ids);
    }

    @InterceptorIgnore(tenantLine = "true")
    @Override
    public List<AppBrand> getBrandByBossId(Integer bossId) {
        LambdaQueryWrapper<AppBossBrand> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(AppBossBrand::getBossId, bossId);
        List<AppBossBrand> appBossBrands = bossBrandMapper.selectList(wrapper);

        List<AppBrand> appBrands = Lists.newArrayList();
        if (CollectionUtils.isNotEmpty(appBossBrands)) {
            Set<Integer> brandIds = appBossBrands.stream().map(AppBossBrand::getBrandId).collect(Collectors.toSet());
            appBrands = this.baseMapper.selectList(Wrappers.<AppBrand>lambdaQuery().in(AppBrand::getId, brandIds));
        }
        return appBrands.stream().sorted(Comparator.comparing(AppBrand::getId).reversed()).collect(Collectors.toList());
    }

}
